<?php
namespace Home\Model;
use Think\Model;
class ArticleModel extends Model{
	private $DescriptionModel;
	function _initialize(){
		parent::_initialize();
		$this->DescriptionModel=M('ArticleDescription');
	}
	
	public function getArticle($article_id){
		$map=array('article_id'=>(int)$article_id);
		$article=$this->where($map)->find();
		$map=array(
			'article_id'	=>	$article_id,
			'language_id'	=>	get_default_lng_id()
			);
		if($article) {
			$article['article_description']=$this->DescriptionModel->where($map)->find();
			$article['article_image']=$this->getArticleImage($article_id);
			}		
		return $article;
	}

	public function getArticleImage($article_id){
		$images=M('ArticleImage')->order('sort_order asc')->where(array('article_id'=>(int)$article_id))->select();
		$article_image=array();
		$language_id=get_default_lng_id();
		foreach($images as $image){
			$text=json_decode($image['text'],true);
			$article_image[]=array(
				'image'	=> $image['image'],
				'text'	=>	$text[$language_id]
				);
		}
		return $article_image;
	}
	
	public function getArticles($data){
		$page=array(
			'start'		=>	$data['start']?$data['start']:0,
			'limit'		=>	$data['limit']?$data['limit']:20
			);
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			'status'		=>	1	
			);
		$order='sort_order ASC,article_id DESC';
		if(isset($data['article_category_id'])) $map['article_category_id']=$data['article_category_id'];
		if(isset($data['title'])) $map['title'] = array('like',"%".$data['title']."%");

		$articles=$this->join('LEFT JOIN  __ARTICLE_DESCRIPTION__ ON __ARTICLE__.article_id=__ARTICLE_DESCRIPTION__.article_id')->where($map)->field(C('DB_PREFIX')."article.article_id,title,meta_description,href,article_category_id,image,date_modified,viewed")->limit($page['start'],$page['limit'])->order($order)->select();
		foreach ($articles as $k => $v) {
			$articles[$k]['url']=U('Article/detail?article_id='.$v['article_id']);
		}
		return $articles;
	}
	public function getCount($data){
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			'status'		=>	1
			);
		if(isset($data['article_category_id'])) $map['article_category_id']=$data['article_category_id'];
		if(isset($data['title'])) $map['title'] = array('like',"%".$data['title']."%");
		$count=$this->join('LEFT JOIN  __ARTICLE_DESCRIPTION__ ON __ARTICLE__.article_id=__ARTICLE_DESCRIPTION__.article_id')->where($map)->count();
		return $count;
	}

	public function getSomeArticles($article_ids=array()){		
		$map=array(
			'language_id'	=>	get_default_lng_id(),
			C('DB_PREFIX').'article.article_id'=>array('in',$article_ids),
			'status'		=>	1	
			);
		$order='sort_order ASC,article_id DESC';
		$articles=$this->join('LEFT JOIN  __ARTICLE_DESCRIPTION__ ON __ARTICLE__.article_id=__ARTICLE_DESCRIPTION__.article_id')->where($map)->field(C('DB_PREFIX')."article.article_id,title,meta_description,href,article_category_id,image,date_modified")->order($order)->select();
		foreach ($articles as $k => $v) {
			$articles[$k]['url']=U('Article/detail?article_id='.$v['article_id']);
		}
		return $articles;
	}
}